package com.business_clean.app.uitl.appupdate.update;

import android.app.Activity;
import android.app.Dialog;

import androidx.core.content.ContextCompat;

import com.business_clean.R;
import com.business_clean.app.callback.OnDialogCancelListener;
import com.business_clean.app.callback.OnDialogConfirmListener;
import com.business_clean.app.ext.CommonUtils;
import com.business_clean.data.mode.appupdate.DialogAlertNoticeInfo;

import org.lzh.framework.updatepluginlib.base.CheckNotifier;

/* 默认使用的在检查到有更新时的通知创建器：创建一个弹窗提示用户当前有新版本需要更新。
 *
 * @author haoge
 * @see CheckNotifier
 */
public class MyUpdateNotifier extends CheckNotifier {
    @Override
    public Dialog create(Activity activity) {
        DialogAlertNoticeInfo dialogAlertNoticeInfo = new DialogAlertNoticeInfo();
        dialogAlertNoticeInfo.setTitle(update.getVersionName() + "版本已经准备好了");
        dialogAlertNoticeInfo.setDescribe("升级一下新功能，也许能帮助到您");
        dialogAlertNoticeInfo.setContent(update.getUpdateContent());
        dialogAlertNoticeInfo.setSub_title_color(ContextCompat.getColor(activity, R.color.base_primary_warning));
        dialogAlertNoticeInfo.setConfirm_button("立即更新");
        dialogAlertNoticeInfo.setDismissOnBackPressed(false);
        dialogAlertNoticeInfo.setDismissOnTouchOutside(false);


        OnDialogCancelListener onDialogCancelListener = new OnDialogCancelListener() {
            @Override
            public void onCancel() {

            }
        };
        if (!update.isForced() && update.isIgnore()) {
            dialogAlertNoticeInfo.setClose_button("忽略此版本");
            onDialogCancelListener = new OnDialogCancelListener() {
                @Override
                public void onCancel() {
                    sendUserIgnore();
                }
            };
        }

        if (!update.isForced()) {
            dialogAlertNoticeInfo.setClose_button("取消");
            onDialogCancelListener = new OnDialogCancelListener() {
                @Override
                public void onCancel() {
                    sendUserCancel();
                }
            };
        }

        OnDialogConfirmListener onDialogConfirmListener = new OnDialogConfirmListener() {
            @Override
            public void onConfirm() {
//                if (update.isForced()) {
//                    preventDismissDialog(dialog);
//                } else {
//                    SafeDialogHandle.safeDismissDialog((Dialog) dialog);
//                }
                sendDownloadRequest();
            }
        };
        CommonUtils.showSystemAlertPop(activity, dialogAlertNoticeInfo, onDialogCancelListener, onDialogConfirmListener);
        return null;
    }
}
